﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mendel
{
    public class String : Data
    {
        public string Value { get; private set; }

        public String(string v)
        {
            Value = System.String.Copy(v);
        }

        public override DType Type
        {
            get { return DType.String; }
        }

        public override Data Clone()
        {
            return new String(Value);
        }

        public override Data Convert(DType to)
        {
            switch (to)
            {
                case DType.Integer:
                    return new Integer(System.Convert.ToInt32(Value));
                case DType.Float:
                    return new Float(System.Convert.ToDouble(Value));
                case DType.String:
                    return this;
                case DType.List:
                    byte status = 1;
                    List<Item> ret = new List<Item>();
                    string s = "";
                    foreach (char ch in Value)
                    {
                        if (Char.IsWhiteSpace(ch))
                        {
                            if (status == 0)
                            {
                                if (!s.Equals(""))
                                {
                                    ret.Add(new Item(new String(s)));
                                    s = "";
                                }
                                status = 1;
                            }
                            else if (status == 2)
                            {
                                s += ch;
                            }
                        }
                        else if (ch == '"')         // ezért nem lehet a beépített Split fv-t használni.
                        {
                            if (status == 0)
                            {
                                Universe.AddError(new Exception("Szintaktikai hiba: \" karakter illegális helyen!"));
                            }
                            else if (status == 1)
                            {
                                status = 2;
                            }
                            else
                            {
                                ret.Add(new Item(new String(s)));
                                s = "";
                                status = 1;
                            }
                        }
                        else
                        {
                            s += ch;

                            if (status == 1)
                                status = 0;
                        }
                    }

                    if(!s.Equals(""))
                        ret.Add(new Item(new String(s)));
                    return new List(ret);
                case DType.Word:
                    return new Word(((List)Convert(DType.List)).Value);
                case DType.Reference:
                    return new Reference(this.Container);
                default:
                    return this;
            }
        }
    }
}
